#include "battle.h"

//人物初始化函数
void InitHero(herodata* hero, char *name, int HP, int POW, int DEF, int location, int num)
{
	strcpy((*hero).name, name);
	(*hero).HP = HP;
	(*hero).POW = POW;
	(*hero).DEF = DEF;
	(*hero).Stun = 0;
	(*hero).location = location;
	(*hero).MP = 0;
	(*hero).victory = 0;
	(*hero).lost = 0;
	(*hero).rate = 0.0;
	(*hero).num = num;
}

//显示人物
void Show_Hero(herolist listdata)
{
	printf("\n");
	for (int i = 0; i < listdata.heronum; i++)
	{
		printf("%d  ", i + 1);
		printf("%s\n", listdata.list[i].name);
		printf("HP: %d  POW:%d  DEF:%d\n", listdata.list[i].HP, listdata.list[i].POW, listdata.list[i].DEF);
		if (listdata.list[i].lost + listdata.list[i].victory != 0)
		{
			listdata.list[i].rate = (float)(listdata.list[i].victory / (listdata.list[i].lost + listdata.list[i].victory));
		}
		printf("Wins:%d  Lose:%d  Rate:%.f\n", listdata.list[i].victory, listdata.list[i].lost, listdata.list[i].rate);
		printf("\n");
	}
}

//主菜单
int Menu(herolist* listdata, herodata* hero1, herodata* hero2)
{

	while (1)
	{
		int select = 0;
		printf("************************************************************************************************************************\n");
		printf("                                            welcome to Final Battle!\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("                     1.开始对战");
		printf("                     2.游戏设置");
		printf("                     3.退出游戏\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("\n");
		printf("************************************************************************************************************************\n");
		scanf("%d", &select);
		if (select == 1)
		{
			printf("请选择你的人物:\n");
			Show_Hero(*listdata);
			scanf("%d", &select);
			*hero1 = listdata->list[select - 1];
			hero1->location = 2;
			printf("请选择你的对手:\n");
			scanf("%d", &select);
			*hero2 = listdata->list[select - 1];
			hero2->location = 4;
			return 0;
		}
		else if (select == 2)
		{
			while (1)
			{
				printf("请输入你要进行的操作:\n");
				printf("1.添加人物\n");
				printf("2.删除人物\n");
				printf("3.显示所有人物\n");
				printf("4.返回上一步\n");
				scanf("%d", &select);
				if (select == 1)//添加人物
				{
					if (listdata->heronum == N)
					{
						printf("英雄已满,请进行其他的操作!\n");
						continue;
					}
					printf("请输入你要添加的人物属性:\n");
					printf("请输入人物名:\n");
					scanf("%s", listdata->list[listdata->heronum].name);
					printf("请输入生命值:\n");
					scanf("%d", &listdata->list[listdata->heronum].HP);
					printf("请输入攻击力:\n");
					scanf("%d", &listdata->list[listdata->heronum].POW);
					printf("请输入防御力:\n");
					scanf("%d", &listdata->list[listdata->heronum].DEF);
					InitHero(&listdata->list[listdata->heronum], listdata->list[listdata->heronum].name, listdata->list[listdata->heronum].HP, listdata->list[listdata->heronum].POW, listdata->list[listdata->heronum].DEF, 0, listdata->heronum);
					listdata->heronum++;
					printf("%d\n", listdata->list[listdata->heronum - 1].num);
					printf("人物添加成功!\n");
				}
				else if (select == 2)//删除人物
				{
					if (listdata->heronum == 0)
					{
						printf("人物为空,无法删除!\n");
						continue;
					}
					char temp[N];
					printf("请输入要删除人物的姓名:\n");
					scanf("%s", temp);
					for (int i = 0; i < N; i++)
					{
						if (strcmp(listdata->list[i].name, temp) == 0)
						{
							for (size_t j = i; j < N - 1; j++)
							{
								listdata->list[j] = listdata->list[j + 1];
							}
							listdata->heronum--;
							break;
						}
					}
					printf("删除成功!\n");
				}
				else if (select == 3)//显示人物
				{
					Show_Hero(*listdata);
				}
				else if (select == 4)//返回
				{
					break;
				}
			}

		}
		else if (select == 3)
		{
			return 1;
		}
	}
	return 0;

}